/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.video.service.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.video.service.entity.VideoListEntity;
import com.hopes.bizz.video.service.service.VideoListService;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.core.util.StringUtils;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 视频
 *
 * @author daijie
 * @date 2023-10-24 13:58:52
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/videoList")
@Tag(description = "videoList", name = "视频管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class VideoListController {

    private final VideoListService videoListService;

    /**
     * 分页查询
     *
     * @param page      分页对象
     * @param videoList 视频
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('video_videoList_view')")
    public R getVideoListPage(Page page, VideoListEntity videoList) {
        LambdaQueryWrapper<VideoListEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StringUtils.isNotBlank(videoList.getName()), VideoListEntity::getName, videoList.getName());

        wrapper.orderByAsc(VideoListEntity::getSortNum);
        return R.ok(videoListService.page(page, wrapper));
    }

    /**
     * 查询全部
     *
     * @param videoList 视频
     * @return
     */
    @Operation(summary = "查询全部", description = "查询全部")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('video_videoList_view')")
    public R getVideoList(VideoListEntity videoList) {
        LambdaQueryWrapper<VideoListEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.orderByAsc(VideoListEntity::getSortNum);
        wrapper.like(StringUtils.isNotBlank(videoList.getName()), VideoListEntity::getName, videoList.getName());
        wrapper.eq(StringUtils.isNotBlank(videoList.getRegionIndex()), VideoListEntity::getRegionIndex, videoList.getRegionIndex());
        wrapper.eq(VideoListEntity::getUseFlag, true);

        return R.ok(videoListService.list(wrapper));
    }

    /**
     * 查询全部带定位的
     *
     * @param videoList 视频
     * @return
     */
    @Operation(summary = "查询全部带定位的", description = "查询全部带定位的")
    @GetMapping("/listLocation")
    @PreAuthorize("@pms.hasPermission('video_videoList_view')")
    public R getvideoListLocation(VideoListEntity videoList) {
        LambdaQueryWrapper<VideoListEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.orderByAsc(VideoListEntity::getSortNum);
        wrapper.like(StringUtils.isNotBlank(videoList.getName()), VideoListEntity::getName, videoList.getName());
        wrapper.eq(StringUtils.isNotBlank(videoList.getRegionIndex()), VideoListEntity::getRegionIndex, videoList.getRegionIndex());
        wrapper.eq(VideoListEntity::getUseFlag, true);
        wrapper.isNotNull(VideoListEntity::getLocation);

        return R.ok(videoListService.list(wrapper));
    }

    /**
     * 视频区域树
     *
     * @param videoList 视频
     * @return
     */
    @Operation(summary = "视频区域树", description = "视频区域树")
    @GetMapping("/tree")
    @PreAuthorize("@pms.hasPermission('video_videoList_view')")
    public R getVideoTree(VideoListEntity videoList) {
        return R.ok(videoListService.videoTree(videoList));
    }

    /**
     * 同步视频
     *
     * @return R
     */
    @Operation(summary = "同步视频", description = "同步视频")
    @SysLog("同步视频区域")
    @GetMapping("/sync")
    @PreAuthorize("@pms.hasPermission('video_videoList_add')")
    public R sync() {
        return R.ok(videoListService.sync());
    }

    /**
     * 通过id查询视频
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('video_videoList_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(videoListService.getById(id));
    }

    /**
     * 新增视频
     *
     * @param videoList 视频
     * @return R
     */
    @Operation(summary = "新增视频", description = "新增视频")
    @SysLog("新增视频")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('video_videoList_add')")
    public R save(@RequestBody VideoListEntity videoList) {
        return R.ok(videoListService.save(videoList));
    }

    /**
     * 修改视频
     *
     * @param videoList 视频
     * @return R
     */
    @Operation(summary = "修改视频", description = "修改视频")
    @SysLog("修改视频")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('video_videoList_edit')")
    public R updateById(@RequestBody VideoListEntity videoList) {
        return R.ok(videoListService.updateById(videoList));
    }

    /**
     * 通过id删除视频
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除视频", description = "通过id删除视频")
    @SysLog("通过id删除视频")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('video_videoList_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(videoListService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param videoList 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('video_videoList_export')")
    public List<VideoListEntity> export(VideoListEntity videoList) {
        return videoListService.list(Wrappers.query(videoList));
    }
}
